#include "app.h"

void test()
{

}


App::App() : App("defalut title")
{
}

void App::init()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	mWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
	mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
	e = new SDL_Event();
}

void App::destroy()
{
	SDL_DestroyRenderer(mRenderer);
	mRenderer = nullptr;
	SDL_DestroyWindow(mWindow);
	mWindow = nullptr;
	SDL_Quit();
}

App::App(const char* title) : title(title), mWindow(nullptr), mRenderer(nullptr), e(nullptr), quit(false), width(800), height(500)
{
}

App::~App()
{
	delete e;
}
 
void App::tick()
{

}

void App::mainLoop()
{
	init();
	while (!quit)
	{
		tick();
		while (SDL_PollEvent(e) != 0)
		{
			switch (e->type)
			{
			case SDL_QUIT:
				quit = true;
				break;
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEMOTION:
			case SDL_MOUSEBUTTONUP:
				for (event_handler handler : event_tables[EVENT_MOUSE])
				{
					(this->*handler)(e);
				}
				break;
			case SDL_KEYDOWN:
			case SDL_KEYUP:
				for (event_handler handler : event_tables[EVENT_KEY])
				{
					(this->*handler)(e);
				}
				break;
			case SDL_WINDOWEVENT:
				SDL_Log("test");
				break;
			default:
				break;
			}
		}
		SDL_Delay(16);
	}
	destroy();
}